﻿package learnmath.windows.elements{
/*-------------------------------------------------------------
	Created by: Ionel Alexandru 
	Mail: ionel.alexandru@gmail.com
	Site: www.learn-math.info
---------------------------------------------------------------*/
import learnmath.windows.*;
import learnmath.windows.util.*;
import learnmath.windows.elements.*;
import learnmath.windows.elements.buttons.*;
import flash.display.*;
import flash.events.*;
import flash.text.*;

public class TaskBar extends Pannel{

	var startButton:ButtonXP;
	var widthStatusText;
	var buttonList:Array;

	var taskBarNextIconX = 0;
	var firstButtonX = 0;
	var maxWidthButton = 100;

	
	private var tf:TextFormat;

	
	public function TaskBar(parent:MovieClip, x:int, y:int, _width:int, _height:int){
		super(parent, x, y, _width, _height);
		buttonList = new Array();

		pannel.graphics.lineStyle();
		pannel.graphics.beginFill(0xd4cfc9);   
		pannel.graphics.drawRect(0, 0, width, height);   
		pannel.graphics.endFill();
		
		pannel.graphics.lineStyle(1,0xffffff);
		pannel.graphics.moveTo(0,1);
		pannel.graphics.lineTo(width, 1);
		
		startButton = new ButtonTextImgXP(pannel, 2, 4, "Config", new StartIconMc(), "STATE");
		firstButtonX = 2 + startButton.width + 8;
		
		var tfs = ConfigManager.getTextFormatForStatus2();
		widthStatusText = TextUtil.getWidth("www.learn-math.info", tfs) + 6;
		var status = new StatusBoxHtml(pannel, width-widthStatusText-3, 4, widthStatusText, height-6, "<a href='http://www.learn-math.info'>www.learn-math.info</a>", tfs);
		
		
	}
	
	public function addIcon(pannelIcon:MovieClip):void{
		pannel.addChild(pannelIcon);
		pannelIcon.x = firstButtonX;
		pannelIcon.y = 7;
		firstButtonX = firstButtonX + 22;
		pannelIcon.buttonMode = true;
		pannelIcon.oldX = pannelIcon.x;
		pannelIcon.oldY = pannelIcon.y;
		pannelIcon.addEventListener(MouseEvent.MOUSE_OVER, drawOver);
		pannelIcon.addEventListener(MouseEvent.MOUSE_OUT, drawOut);
	}
	
	function drawOut(event:MouseEvent):void {
		event.target.x = event.target.oldX;
		event.target.y = event.target.oldY;
	}
	
	function drawOver(event:MouseEvent):void {
		event.target.x = event.target.oldX - 1;
		event.target.y = event.target.oldY - 1;
	}		
	
	public function addButton(pannelIcon:MovieClip, name:String):ButtonTextImgWidthXP{
		var button = new ButtonTextImgWidthXP(pannel, firstButtonX, 4, name, pannelIcon, "STATE", maxWidthButton);
		buttonList[buttonList.length] = button;
		recalcWidthButtons();
		button.drawDOWN();
		return button;
	}

	public function removeButton(id:int):void{
		var button  = buttonList[id];
		pannel.removeChild(button.pannel);
		buttonList[id] = null;
		recalcWidthButtons();
	}


	public function recalcWidthButtons():void{
		var maxWidth = width - firstButtonX - widthStatusText - 3;
		
		var nbButtons = 0;
		for(var i=0; i<buttonList.length; i++){
			if(buttonList[i]!=null){
				nbButtons++;
			}
		}
		var buttonWidth = maxWidthButton;
		
		if(nbButtons*maxWidthButton>maxWidth){
			buttonWidth = Math.floor(maxWidth/nbButtons);
		}
		var buttonX = 0;
		for(i=0; i<buttonList.length; i++){
			if(buttonList[i]!=null){
				var button = buttonList[i];
				button.setX(firstButtonX + buttonX)
				button.setWidth(buttonWidth-2);
				button.drawUP();
				buttonX = buttonX + buttonWidth;
			}
		}
	}
}
}